package com.ruoyi.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.user.domain.UserRelation;
import com.ruoyi.user.domain.bo.UserRelationBo;
import com.ruoyi.user.domain.vo.UserRelationVo;
import com.ruoyi.user.mapper.UserRelationMapper;
import com.ruoyi.user.service.IUserRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 人员社会关系Service业务层处理
 *
 * @author strongme
 * @date 2023-01-26
 */
@RequiredArgsConstructor
@Service
public class UserRelationServiceImpl implements IUserRelationService {

    private final UserRelationMapper baseMapper;

    /**
     * 查询人员社会关系
     */
    @Override
    public UserRelationVo queryById(Long relationId) {
        return baseMapper.selectVoById(relationId);
    }

    @Override
    public List<UserRelationVo> queryByUserId(Long userId) {
        return baseMapper.selectVoList(Wrappers.<UserRelation>lambdaQuery().eq(UserRelation::getUserId, userId));
    }

    /**
     * 查询人员社会关系列表
     */
    @Override
    public TableDataInfo<UserRelationVo> queryPageList(UserRelationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserRelation> lqw = buildQueryWrapper(bo);
        Page<UserRelationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询人员社会关系列表
     */
    @Override
    public List<UserRelationVo> queryList(UserRelationBo bo) {
        LambdaQueryWrapper<UserRelation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserRelation> buildQueryWrapper(UserRelationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserRelation::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), UserRelation::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getIdno()), UserRelation::getIdno, bo.getIdno());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), UserRelation::getGender, bo.getGender());
        lqw.eq(bo.getBirthday() != null, UserRelation::getBirthday, bo.getBirthday());
        lqw.eq(StringUtils.isNotBlank(bo.getWhcd()), UserRelation::getWhcd, bo.getWhcd());
        lqw.eq(StringUtils.isNotBlank(bo.getJtdz()), UserRelation::getJtdz, bo.getJtdz());
        lqw.eq(StringUtils.isNotBlank(bo.getDwyzy()), UserRelation::getDwyzy, bo.getDwyzy());
        lqw.eq(StringUtils.isNotBlank(bo.getXhgx()), UserRelation::getXhgx, bo.getXhgx());
        lqw.eq(StringUtils.isNotBlank(bo.getLxdh()), UserRelation::getLxdh, bo.getLxdh());
        return lqw;
    }

    /**
     * 新增人员社会关系
     */
    @Override
    public Boolean insertByBo(UserRelationBo bo) {
        UserRelation add = BeanUtil.toBean(bo, UserRelation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRelationId(add.getRelationId());
        }
        return flag;
    }

    /**
     * 修改人员社会关系
     */
    @Override
    public Boolean updateByBo(UserRelationBo bo) {
        UserRelation update = BeanUtil.toBean(bo, UserRelation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserRelation entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除人员社会关系
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
